package de.unikoblenz.west.csxpoi;

import de.unikoblenz.west.csxpoi.R;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageButton;
/**
 * creates help button
 *
 */
public class HelpButton extends ImageButton {

	/**
	 * animations constants in milliseconds
	 */
	private final static int DURATION = 3000, ANIMATION_TIME = 300;

	/**
	 * a static variable of this {@link HelpButton}
	 */
	private static HelpButton helpButton;

	/**
	 * {@link Handler} for the timer
	 */
	private static Handler mHandler = new Handler();

	/**
	 * fade in- and out animations
	 */
	private static Animation mFadeIn, mFadeOut;

	/**
	 * help button context
	 */
	private Context mContext;

	
	public HelpButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public HelpButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public HelpButton(Context context) {
		super(context);
		init(context);
	}

	/**
	 * Initializes help button
	 * @param context
	 */
	private void init(Context context) {
		helpButton = this;
		mContext = context;
		
		this.setImageDrawable(getResources().getDrawable(R.drawable.ic_menu_help));
		this.setBackgroundColor(Color.TRANSPARENT);
		
		// initialize animations
		mFadeIn = new AlphaAnimation(0, 1);
		mFadeIn.setDuration(ANIMATION_TIME);
		mFadeOut = new AlphaAnimation(1, 0);
		mFadeOut.setDuration(ANIMATION_TIME);

		this.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mContext
						.startActivity(new Intent(mContext, HelpActivity.class));
			}
		});
	}

	/**
	 * shows help button (e.g. map is touched)
	 */
	public static void setActive() {
		if (helpButton.getVisibility() != View.VISIBLE)
			helpButton.startAnimation(mFadeIn);
		helpButton.setVisibility(View.VISIBLE);
		// start timer
		mHandler.removeCallbacks(hideSeekBar);
		mHandler.postDelayed(hideSeekBar, DURATION);
	}

	/**
	 * If this {@link Runnable} is started, the {@link HelpActivity} will fade
	 * out and be invisible.
	 */
	private static Runnable hideSeekBar = new Runnable() {
		public void run() {
			helpButton.startAnimation(mFadeOut); 
			helpButton.setVisibility(View.GONE);
			mHandler.removeCallbacks(hideSeekBar);
		}
	};
}
